package br.edu.univas.locadoragraph.bean;



import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.component.html.HtmlInputText;
import javax.faces.context.FacesContext;

import org.primefaces.model.DualListModel;



import br.edu.univas.locadoragraph.dao.ActorDAO;
import br.edu.univas.locadoragraph.dao.MovieDAO;
import br.edu.univas.locadoragraph.to.MovieTO;

@ManagedBean(name="filmeBean")
@SessionScoped
public class FilmeBean implements Serializable{
	private static final long serialVersionUID = 1L;
	

	private String nome;
	private String year;
	private MovieTO movieTo;
	private MovieDAO movieDAO;
	
	
	private List<MovieTO> listFilmes;
    private MovieTO selectedFilme;
    private DualListModel<String> listActors;
	private ActorDAO actorDao;
	
	private HtmlInputText cleanField;
	
	public FilmeBean(){  
       movieTo = new MovieTO();
       movieDAO = new MovieDAO();
       actorDao = new ActorDAO();
       
       getContructListActors();
   }
	
	
	public void saveMovie(){
		setListActorSelected();
		System.out.println(this.movieTo.getTitle());
		
		if(this.movieDAO.saveMovieGraph(this.movieTo)){
			FacesContext.getCurrentInstance().addMessage(null,new FacesMessage("Filme Cadastrado com Sucesso!")); 
		}else{
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Houve algum erro no cadastro do filme!", this.movieTo.getTitle()));  
		}
		
		
		
		this.limpaCampos();
	}
	
	public void setListActorSelected(){
		ArrayList<String> arrayActor = new ArrayList<String>();
		for (String ator : listActors.getTarget()) {
            System.out.println("Esta cor é: " + ator);
            arrayActor.add(ator);   
        }
		movieTo.setActors(arrayActor);
	}
	
	 public void getContructListActors(){
    	 List<String> actorsSource = new ArrayList<String>();  
         List<String> actorsTarget = new ArrayList<String>();
           
         actorsSource = actorDao.getAllActors(); 
         /*for (ActorTO ator :actorsSource) {
        	 	ator.getName();
        	 	actorsSource.add(ator);
		}*/
         listActors = new DualListModel<String>(actorsSource, actorsTarget); 
    }  
	
	
	
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	
	/*Não ha necessidade de ter get and set Year, referencia To*/
	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}
	
	public void limpaCampos(){
		this.nome = "";
		//this.movieTO.setGender(" ");
		this.movieTo.setYear(0);
	}

	/*Limpa Campos*/
	public HtmlInputText getCleanField() {
		return cleanField;
	}


	public void setCleanField(HtmlInputText cleanField) {
		this.cleanField = cleanField;
	}


	/*Responsavei pela transição Bean and To*/
	public MovieTO getMovieTO() {
		return movieTo;
	}
	public void setMovieTO(MovieTO movieTO) {
		this.movieTo = movieTO;
	}
	public MovieDAO getMovieDAO() {
		return movieDAO;
	}
	public void setMovieDAO(MovieDAO movieDAO) {
		this.movieDAO = movieDAO;
	}


	public List<MovieTO> getListFilmes() {
		return listFilmes;
	}


	public void setListFilmes(List<MovieTO> listFilmes) {
		this.listFilmes = listFilmes;
	}


	public MovieTO getSelectedFilme() {
		return selectedFilme;
	}


	public void setSelectedFilme(MovieTO selectedFilme) {
		this.selectedFilme = selectedFilme;
	}


	public DualListModel<String> getListActors() {
		return listActors;
	}


	public void setListActors(DualListModel<String> listActors) {
		this.listActors = listActors;
	}


	public ActorDAO getActorDao() {
		return actorDao;
	}


	public void setActorDao(ActorDAO actorDao) {
		this.actorDao = actorDao;
	}
	
}